////////////////////////////////////////////////
// MACHINE GENERATED, PLEASE DONOT MODIFY!    //
////////////////////////////////////////////////

#include "lua_bind_rpc_generated.hh"
#include "../../lua/user/lua_bind.hh"
#include "../../lua/user/lua_helper.hh"
#include "../../lua/user/lua_entity.hh"
#include "../../plugin/base/entity.hh"
#include "rpc_generated.hh"
#include <memory>

int callRPCPathISampleEcho(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto pathID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Path::getSample(pathID)->echo(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

int callRPCEntityISampleEcho(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto entityID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Entity::getSample(entityID)->echo(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

bool LuaRPC::luaCallISampleEcho(std::uint64_t pathID, std::uint64_t fromPathID, std::uint64_t entityID, std::uint64_t uuid, std::uint64_t rpcID, const google::protobuf::Message& req, google::protobuf::Message& ack) {
    if (entityID) {
        auto entity = EntityManagerRef.getEntityByType<LuaEntity>(entityID);
        if (entity) {
            return entity->callLuaImplMethod(pathID, fromPathID, uuid, rpcID, "Sample", "echo", req, ack);
        }
    }
    return LuaRef.callLuaImplMethod("Sample", "echo", pathID, uuid, rpcID, req, ack);
}

int callRPCPathISampleEcho1(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto pathID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Path::getSample(pathID)->echo1(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

int callRPCEntityISampleEcho1(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto entityID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Entity::getSample(entityID)->echo1(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

bool LuaRPC::luaCallISampleEcho1(std::uint64_t pathID, std::uint64_t fromPathID, std::uint64_t entityID, std::uint64_t uuid, std::uint64_t rpcID, const google::protobuf::Message& req, google::protobuf::Message& ack) {
    if (entityID) {
        auto entity = EntityManagerRef.getEntityByType<LuaEntity>(entityID);
        if (entity) {
            return entity->callLuaImplMethod(pathID, fromPathID, uuid, rpcID, "Sample", "echo1", req, ack);
        }
    }
    return LuaRef.callLuaImplMethod("Sample", "echo1", pathID, uuid, rpcID, req, ack);
}

int callRPCPathISample1Echo(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto pathID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Path::getSample1(pathID)->echo(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

int callRPCEntityISample1Echo(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto entityID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Entity::getSample1(entityID)->echo(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

bool LuaRPC::luaCallISample1Echo(std::uint64_t pathID, std::uint64_t fromPathID, std::uint64_t entityID, std::uint64_t uuid, std::uint64_t rpcID, const google::protobuf::Message& req, google::protobuf::Message& ack) {
    if (entityID) {
        auto entity = EntityManagerRef.getEntityByType<LuaEntity>(entityID);
        if (entity) {
            return entity->callLuaImplMethod(pathID, fromPathID, uuid, rpcID, "Sample1", "echo", req, ack);
        }
    }
    return LuaRef.callLuaImplMethod("Sample1", "echo", pathID, uuid, rpcID, req, ack);
}

int callRPCPathISample1Echo1(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto pathID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Path::getSample1(pathID)->echo1(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

int callRPCEntityISample1Echo1(lua_State * L) {
    DummyReq req;
    copyTableLuaToC(L, req);
    lua_pop(L, 1);
    auto timeout = (std::uint64_t)lua_tointeger(L, -1);
    auto entityID = (std::uint64_t)lua_tointeger(L, -2);
    auto result = RPCSkeleton::Entity::getSample1(entityID)->echo1(req,
        [L](const Message& ack) {
            onCallAck(L, ack);           
        },
        [L]() {
            onCallTimeout(L);
        },
        timeout
        );
    if (!result) {
        lua_pushnil(L);
        return 1;
    }
    return lua_yieldk(L, 0, 0, nullptr);
}

bool LuaRPC::luaCallISample1Echo1(std::uint64_t pathID, std::uint64_t fromPathID, std::uint64_t entityID, std::uint64_t uuid, std::uint64_t rpcID, const google::protobuf::Message& req, google::protobuf::Message& ack) {
    if (entityID) {
        auto entity = EntityManagerRef.getEntityByType<LuaEntity>(entityID);
        if (entity) {
            return entity->callLuaImplMethod(pathID, fromPathID, uuid, rpcID, "Sample1", "echo1", req, ack);
        }
    }
    return LuaRef.callLuaImplMethod("Sample1", "echo1", pathID, uuid, rpcID, req, ack);
}

bool LuaRPC::installRPCProxyLibrary() {
	auto luaState = LuaRef.getVirtualMachine();
	if (!installRPCMethod(luaState, "RPC.Path.Sample.echo", &callRPCPathISampleEcho)) {
		errorlog << "Install RPC method 'RPC.Path.Sample.echo' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Entity.Sample.echo", &callRPCEntityISampleEcho)) {
		errorlog << "Install RPC method 'RPC.Entity.Sample.echo' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Path.Sample.echo1", &callRPCPathISampleEcho1)) {
		errorlog << "Install RPC method 'RPC.Path.Sample.echo1' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Entity.Sample.echo1", &callRPCEntityISampleEcho1)) {
		errorlog << "Install RPC method 'RPC.Entity.Sample.echo1' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Path.Sample1.echo", &callRPCPathISample1Echo)) {
		errorlog << "Install RPC method 'RPC.Path.Sample1.echo' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Entity.Sample1.echo", &callRPCEntityISample1Echo)) {
		errorlog << "Install RPC method 'RPC.Entity.Sample1.echo' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Path.Sample1.echo1", &callRPCPathISample1Echo1)) {
		errorlog << "Install RPC method 'RPC.Path.Sample1.echo1' failed" << endlog;
		return false;
	}
	if (!installRPCMethod(luaState, "RPC.Entity.Sample1.echo1", &callRPCEntityISample1Echo1)) {
		errorlog << "Install RPC method 'RPC.Entity.Sample1.echo1' failed" << endlog;
		return false;
	}
	return true;
}

bool LuaRPC::uninstallRPCProxyLibrary() {
	uninstallRPCMethod(LuaRef.getVirtualMachine(), "RPC");
	return true;
}
